<template>
  <div>
    <bread second="商品管理" third="参数列表"></bread>
    <el-card class="box-card" style="width:100%;margin-top:10px">
      <el-alert title="注意：只允许为第三级分类设置相关参数!" type="warning" show-icon></el-alert>
      <!-- :model="form" -->
      <el-form ref="roleForm" style="margin:15px 0">
        <el-form-item label="选择商品分类:">
          <div class="block">
            <el-cascader
              v-model="value"
              :options="options"
              :props="cascaderProps"
              @change="handleChange"
              :clearable="true"
              ref="cascaderForm"
            ></el-cascader>
          </div>
        </el-form-item>
        <el-tabs v-model="activeName" @tab-click="handleClick">
          <el-tab-pane name="many">
            <span slot="label">动态参数</span>
            <el-button
              type="primary"
              size="mini"
              :disabled="disabled"
              @click="dialogFormVisible2 = true"
            >添加参数</el-button>
            <el-table :data="tableData1" border style="width: 100%;margin-top:10px">
              <el-table-column type="expand">
                <template slot-scope="scoped">
                  <el-tag
                    :key="index"
                    v-for="(tag,index) in scoped.row.attr_vals"
                    closable
                    :disable-transitions="false"
                    @close="handleClose(index,tag,scoped.row)"
                  >{{tag}}</el-tag>
                  <el-input
                    class="input-new-tag"
                    v-if="scoped.row.inputVisible"
                    v-model="scoped.row.inputValue"
                    ref="saveTagInput"
                    size="small"
                    @keyup.enter.native="handleInputConfirm((scoped.row))"
                    @blur="handleInputConfirm(scoped.row)"
                  ></el-input>
                  <el-button
                    v-else
                    class="button-new-tag"
                    size="small"
                    @click="showInput(scoped.row)"
                  >+ New Tag</el-button>
                </template>
              </el-table-column>
              <el-table-column type="index" label="#"></el-table-column>
              <el-table-column prop="attr_name" label="参数名称"></el-table-column>
              <el-table-column label="操作">
                <template slot-scope="scoped">
                  <el-button type="primary" size="mini" @click="editParams(scoped.row)">
                    <i class="el-icon-edit"></i>编辑
                  </el-button>
                  <el-button size="mini" type="danger" @click="handleDelete(scoped.row)">
                    <i class="el-icon-delete"></i>删除
                  </el-button>
                </template>
              </el-table-column>
            </el-table>
          </el-tab-pane>

          <el-tab-pane name="only">
            <span slot="label">静态属性</span>
            <el-button
              type="primary"
              size="mini"
              :disabled="disabled"
              @click="dialogFormVisible3 = true"
            >添加属性</el-button>
            <el-table :data="tableData2" border style="width: 100%;margin-top:10px">
              <el-table-column type="expand">
                <template slot-scope="scoped">
                  <el-tag
                    :key="index"
                    v-for="(tag,index) in scoped.row.attr_vals.split(',')"
                    closable
                    :disable-transitions="false"
                    @close="handleClose(tag,scoped.row.attr_vals)"
                  >{{tag}}</el-tag>
                  <el-input
                    class="input-new-tag"
                    v-if="scoped.row.inputVisible"
                    v-model="editCateparams.attr_vals"
                    ref="saveTagInput"
                    size="small"
                    @keyup.enter.native="handleInputConfirm((scoped.row))"
                    @blur="handleInputConfirm(scoped.row)"
                  ></el-input>
                  <el-button
                    v-else
                    class="button-new-tag"
                    size="small"
                    @click="showInput(scoped.row)"
                  >+ New Tag</el-button>
                </template>
              </el-table-column>
              <el-table-column type="index" label="#"></el-table-column>
              <el-table-column prop="attr_name" label="属性名称"></el-table-column>
              <el-table-column label="操作">
                <template>
                  <el-button type="primary" size="mini">
                    <i class="el-icon-edit"></i>编辑
                  </el-button>
                  <el-button size="mini" type="danger">
                    <i class="el-icon-delete"></i>删除
                  </el-button>
                </template>
              </el-table-column>
            </el-table>
          </el-tab-pane>
        </el-tabs>
      </el-form>
    </el-card>

    <!-- 添加动态 -->
    <el-dialog title="添加动态属性" :visible.sync="dialogFormVisible2">
      <el-form :model="form" :rules="rules" ref="formName">
        <el-form-item label="动态属性" label-width="100px" prop="attr_name">
          <el-input v-model="form.attr_name" auto-complete="off"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible2 = false">取 消</el-button>
        <el-button type="primary" @click="addParams('formName')">确 定</el-button>
      </div>
    </el-dialog>
    <!-- 添加静态属性 -->
    <el-dialog title="添加静态属性" :visible.sync="dialogFormVisible3">
      <el-form :model="form" :rules="rules" ref="formName">
        <el-form-item label="静态参数" label-width="100px" prop="attr_name">
          <el-input v-model="form.attr_name" auto-complete="off"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible3 = false">取 消</el-button>
        <el-button type="primary" @click="addParams('formName')">确 定</el-button>
      </div>
    </el-dialog>
    <!-- 编辑动态属性 -->
    <el-dialog title="编辑动态属性" :visible.sync="dialogFormVisible4">
      <el-form :model="form" :rules="rules" ref="formName">
        <el-form-item label="动态属性" label-width="100px" prop="attr_name">
          <el-input v-model="form.attr_name" auto-complete="off"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible4 = false">取 消</el-button>
        <el-button type="primary" @click="adParams('formName')">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
/* eslint-disable */
import { cateList_api, getCateParams_api, addParams_api, delCateParams_api, editCateParams_api } from '@/api/index.js'
export default {
  data() {
    return {
      tableData1: [],//表格1数据
      tableData2: [],//表格2数据
      // disabled: true,//按钮禁用
      value: [],//级联选择器中选中的数据的id
      options: [],//级联选择器中的数据
      cascaderProps: {
        value: 'cat_id',
        label: 'cat_name',
        children: 'children',
        expandTrigger: 'hover'
      },
      activeName: 'many',
      dialogFormVisible: false,
      dialogFormVisible2: false,
      dialogFormVisible3: false,
      dialogFormVisible4: false,
      form: {
        id: '',
        attrId: '',
        attr_name: '',
        attr_vals: '',
      },
      rules: {
        attr_name: [
          { required: true, message: '请输入动态属性', trigger: 'blur' }
        ]
      },
      editCateparams: {
        attr_vals: ''//参数的属性值
      },
    }
  },
  created() {
    this.getCatename()
  },

  methods: {
    //级联选择器中值发生改变时触发的事件
    async handleChange(value) {
      // console.log(value, '11111')
      //任意选择级联选择器一项就关闭下拉框
      this.$refs.cascaderForm.dropDownVisible = false
      if (value.length === 3) {
        //最后一项选择了才解禁按钮
        // this.disabled = false
        //解禁按钮后发送请求获取动态表格数据
        this.paramsFunc()
      } else {
        // this.disabled = true
        this.value = []
      }

    },
    //发送请求获取参数的封装
    async paramsFunc() {
      //获取最后一项的id  
      // var id = this.value[this.value.length - 1]
      console.log(this.activeName, 9);
      const { data: res } = await getCateParams_api({ id: this.cateID, sel: this.activeName })
      console.log(res.data);
      //获取参数列表时往里面添加inputVisible属性作为唯一标识
      res.data.forEach(item => {
        item.inputVisible = false
        item.inputValue = ''
        item.attr_vals = item.attr_vals.split(',')

      });
      console.log(res.data);
      console.log(res, 4);
      // console.log(this.activeName, 9);
      console.log(res, 123);
      if (res.meta.status === 200) {
        if (this.activeName === 'many') {
          this.tableData1 = res.data
        } else {
          this.tableData2 = res.data
        }
      }
    },
    //点击tab栏切换中的事件
    handleClick(tab, event) {
      // console.log(tab, event, '9999')
      this.paramsFunc()
    },
    //获取参数列表
    async getCatename() {
      const { data: res } = await cateList_api({ type: 3 })
      // console.log(res, 'mm')
      this.options = res.data
    },
    //删除商品
    async handleDelete(row) {
      const res2 = await this.$confirm(
        '此操作将永久删除该文件, 是否继续?', '提示',
        {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }
      ).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
      if (res2 == 'confirm') {
        const { data: res } = await delCateParams_api({ id: row.cat_id, attrid: row.attr_id })
        this.paramsFunc()
      }
    },
    //添加属性
    async handleClose(i, tag, row) {
      row.attr_vals.splice(i, 1);
      this.ad(row)
    },
    showInput(row) {
      row.inputVisible = true;
      //$nextTick等页面结构重新渲染完后在执行
      this.$nextTick(_ => {
        //表单自动聚焦
        this.$refs.saveTagInput.$refs.input.focus();
      });
    },
    async ad(row) {
      var params = {
        id: row.cat_id,
        attrId: row.attr_id,
        attr_name: row.attr_name,
        attr_sel: this.activeName,
        attr_vals: [...row.attr_vals, row.inputValue].join(',')
      }
      const { data: res } = await editCateParams_api(params)
      if (res.meta.status == 200) {
        this.$message({
          message: res.meta.msg,
          type: 'success'
        })

      }
    },
    handleInputConfirm(row) {
      row.inputVisible = false;
      // row.attr_vals = row.attr_vals.concat(`${this.editCateparams.attr_vals}`)
      // console.log(row, row.attr_vals, '000000');
      if (row.inputValue.trim().length === 0) {
        return
      }
      row.attr_vals.push(row.inputValue)
      row.inputValue = ''
      this.ad(row)
    },

    //添加动态和静态属性
    //添加封装
    async add(param) {
      const { data: res } = await addParams_api(param)
      if (res.meta.status === 201) {
        this.$message({
          message: res.meta.msg,
          type: 'success'
        })
        this.paramsFunc()
        // this.form.attr_name = ''
        this.$refs.formName.resetFields();
      }
    },
    addParams(formName) {
      this.$refs[formName].validate(valid => {
        if (!valid) {
          return
        } else {
          this.dialogFormVisible2 = false
          var id = this.value[this.value.length - 1]
          // console.log(this.value.join(','));
          if (this.activeName == 'many') {
            this.add({
              id,
              attr_name: this.form.attr_name,
              attr_sel: this.activeName,
              attr_vals: this.value.join(',')
            })
          } else {
            this.add({
              id,
              attr_name: this.form.attr_name,
              attr_sel: this.activeName,
            })
            this.dialogFormVisible3 = false
          }

        }
      })
    },
    //编辑提交参数
    editParams(row) {
      this.dialogFormVisible4 = true
      // console.log(row, 123);
      this.form = row
      this.form.id = row.cat_id
      this.form.attrId = row.attr_id
    },
    adParams(formName) {
      this.$refs[formName].validate(async valid => {
        if (!valid) {
          return
        } else {
          this.form
          const { data: res } = await editCateParams_api({
            id: this.form.id,
            attrId: this.form.attrId,
            attr_name: this.form.attr_name,
            attr_sel: this.activeName,
            attr_vals: this.form.attr_vals
          })
          if (res.meta.status == 200) {
            this.$message({
              message: res.meta.msg,
              type: 'success'
            })
            this.dialogFormVisible4 = false
          }
        }
      })
    }
  },
  computed: {
    //使用计算属性获取按钮的禁用状态
    disabled() {
      if (this.value.length === 3) {
        return false
      } else {
        return true
      }
    },
    //使用计算属性获取级联选择器的分类ID
    cateID() {
      if (this.value.length === 3) {
        return this.value[2]
      } else {
        return null
      }
    }
  },

}
</script>
<style lang="less" scoped>
.el-tag + .el-tag {
  margin-left: 10px;
}
.button-new-tag {
  margin-left: 10px;
  height: 32px;
  line-height: 30px;
  padding-top: 0;
  padding-bottom: 0;
}
.input-new-tag {
  width: 90px;
  margin-left: 10px;
  vertical-align: bottom;
}
</style>
